package com.block.job.service;

import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.db.mapper.SourceDataCountInfoMapper;
import com.block.db.mapper.SourcePushResultInfoMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ChannelExitJob {

    private static final Logger log = LogFactory.getJobLog();

    @Resource
    SourceDataCountInfoMapper sourceDataCountInfoMapper;


    @Autowired
    NotifyExceptionService notifyExceptionService;

    /**
     * 查询渠道
     */
    @XxlJob("QueryChannelJob")
    public void query() throws Exception {
        XxlJobHelper.log("查询渠道---------开始执行--------");

        List<Map<String, Object>> openChannelList = sourceDataCountInfoMapper.selectOpenChannel();
        List<Map<String, Object>> onlineChannelList = sourceDataCountInfoMapper.selectOnlineChannel();

        Map<String, String> openChannel = openChannelList.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("key"),
                        map -> (String) map.get("value")
                ));
        Map<String, String> onlineChannel = onlineChannelList.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("key"),
                        map -> (String) map.get("value")
                ));


        StringBuilder messageBuilder = new StringBuilder();
        for (Map.Entry<String, String> entry : openChannel.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if (!onlineChannel.containsKey(key)) {
                messageBuilder.append("未在线的渠道：").append(value).append("\n");
            }
        }

        String message = messageBuilder.toString();
        // 将消息通知发送到企业微信
        notifyExceptionService.notifyWechatShibaiException("MERCHANT_NOTICE", "", message, null);
    }
}
